// @ts-nocheck
import tseslint from 'typescript-eslint';
import eslint from '@eslint/js';
import prettier from 'eslint-plugin-prettier';
import importSort from 'eslint-plugin-simple-import-sort';
import globals from 'globals';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';

const ignores = [
  'node_modules',
  'dist',
  'build',
  '**/*.mjs',
  '**/*.d.ts',
  'eslint.config.js',
  'commitlint.config.js',
  'apps/backend/video/dist/**/*',
];

// 👇 关键：让 eslint-plugin-prettier 读取 .prettierrc
const prettierOptions = {
  usePrettierrc: true, // ← 这行最重要！
};

const commonConfig = {
  ignores,
  extends: [eslint.configs.recommended, ...tseslint.configs.recommended],
  rules: {
    'no-console': 'error',
    'prettier/prettier': ['error', prettierOptions],
    'simple-import-sort/imports': 'error',
    'simple-import-sort/exports': 'error',
  },
  plugins: {
    prettier,
    'simple-import-sort': importSort,
  },
};

const frontendConfig = {
  files: ['apps/frontend/**/*.{ts,tsx}', 'packages/**/*.{ts,tsx}'],
  languageOptions: {
    ecmaVersion: 2020,
    globals: globals.browser,
    parser: tseslint.parser, // 👈 必须显式指定！
    parserOptions: {
      project: true,
      tsconfigRootDir: import.meta.dirname, // 同样指向根目录
    },
  },
  plugins: {
    'react-hooks': reactHooks,
    'react-refresh': reactRefresh,
  },
  rules: {
    ...reactHooks.configs.recommended.rules,
    'react-refresh/only-export-components': 'warn',
    'no-console': 'off',
  },
};

const backendConfig = {
  files: ['apps/backend/**/*.{ts,tsx}'], // 👈 必须加 files！
  languageOptions: {
    ecmaVersion: 2020,
    globals: {
      ...globals.node,
    },
    parser: tseslint.parser,
    parserOptions: {
      project: true, // 自动找最近的 tsconfig.json
      tsconfigRootDir: import.meta.dirname, // 👈 关键！设为当前配置文件所在目录（即 monorepo 根）
    },
  },
  rules: {
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/interface-name-prefix': 'off',
    'no-explicit-any': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
  },
};
export default tseslint.config(commonConfig, frontendConfig, backendConfig);
